package cool.taomu.toolkit.kademlia.entity

import cool.taomu.toolkit.kademlia.thrift.Response
import java.util.ArrayList
import java.util.List
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.ToString

@Accessors
@ToString
class KademliaReponse {
	List<KademliaNode> nodes;
	String result;
	int status;

	new(Response response) {
		this.result = response.result;
		this.status = response.status;
		this.nodes = new ArrayList();
		if (response.nodes !== null) {
			response.nodes.forEach [
				this.nodes.add(new KademliaNode(it))
			]
		}
	}

	def toResponse() {
		val resp = new Response();
		resp.status = this.status
		resp.result = this.result;
		resp.nodes = new ArrayList();
		if (this.nodes !== null) {
			this.nodes.forEach [
				resp.nodes.add(it.toNode)
			]
		}
		return resp;
	}

	new(List<KademliaNode> nodes, String result, int status) {
		this.nodes = nodes;
		this.result = result;
		this.status = status;
	}

	new(List<KademliaNode> nodes, byte[] result, int status) {
		this.nodes = nodes;
		this.result = new String(result, "UTF-8");
		this.status = status;
	}

	new(String result, int status) {
		this(null, result, status);
	}

	new(String result) {
		this(null, result, 200);
	}

	new(List<KademliaNode> nodes, String result) {
		this(nodes, result, 200)
	}

	new() {
	}

}
